package com.yuzhitong.shapi.widget;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.base.recycler.BaseAdapter;
import com.yuzhitong.shapi.base.recycler.BaseViewHolder;
import com.yuzhitong.shapi.bean.PlayListBean;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/4/19  16:30
 * Description:
 * 反馈建议弹框
 */
public class PlayTypeDialog extends CenterDialog implements View.OnClickListener {
    private Context context;
    private View rootView;
    private TextView textCancel;

    private List<PlayListBean.PlayerListDTO> infoList;

    private BaseAdapter<PlayListBean.PlayerListDTO> typeAdapter;
    private OnSelectItemListener onSelectItemListener;
    private RecyclerView rlPlayTypeList;

    public PlayTypeDialog(Context context) {
        super(context);
        this.context = context;
        Window win = alertDialog.getWindow();
        win.setGravity(Gravity.BOTTOM);
        win.getDecorView().setPadding(0, 0, 0, 0);
        win.getAttributes().width = MATCH_PARENT;

        setContentView(rootView = LayoutInflater.from(context).inflate(R.layout.dialog_play_type, null, false));

        initView();
        setKeyListener(true);
    }

    private void initView() {

        textCancel = (TextView) rootView.findViewById(R.id.text_cancel);

        textCancel.setOnClickListener(this);
        rlPlayTypeList = rootView.findViewById(R.id.rl_play_type_list);


        typeAdapter = new BaseAdapter<PlayListBean.PlayerListDTO>() {
            @Override
            public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(getContext()).inflate(R.layout.item_dialog_play_type, parent, false);
                return new BaseViewHolder(itemView);
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int RealPosition, PlayListBean.PlayerListDTO data) {
                TextView tv = viewHolder.findViewById(R.id.tv_play_type_item);
                tv.setText(data.getPlayerName());
            }
        };

        typeAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<PlayListBean.PlayerListDTO>() {
            @Override
            public void onItemClick(int position, PlayListBean.PlayerListDTO data) {
                dismiss();
                if (onSelectItemListener != null) {
                    onSelectItemListener.onSelect(position, data);
                }
            }
        });

        rlPlayTypeList.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, true));
        rlPlayTypeList.setAdapter(typeAdapter);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.text_cancel) {
            dismiss();
            if (onSelectItemListener != null) {
                onSelectItemListener.onDismiss();
            }
        }
    }

    @Override
    public void show() {
        super.show();
    }

    public PlayTypeDialog setInfoList(List<PlayListBean.PlayerListDTO> infoList) {
        if (infoList == null) {
            return this;
        }
        this.infoList = infoList;
        typeAdapter.setDataList(infoList);
        typeAdapter.notifyDataSetChanged();
        return this;
    }

    public PlayTypeDialog setOnSelectItemListener(OnSelectItemListener onSelectItemListener) {
        this.onSelectItemListener = onSelectItemListener;
        return this;
    }


    public interface OnSelectItemListener {
        void onDismiss();

        void onSelect(int position, PlayListBean.PlayerListDTO infoBea);
    }
}
